<?php


namespace Modules\Chinadmin\Services;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\TemplateProcessor;

/**
 * word文件操作
 * Class WordService
 * @package Modules\Chinadmin\Services
 * @author 张秦萁
 * @email  993187039@qq.com
 */
class WordService
{


    /**
     * 创建word文件
     * @param $dataArray array('name' => '小黑', 'title' => '小红')
     * @param string $templateFilePath
     * @param $isDownload
     * @return string
     * 换行符 <w:br/>
     * 分页符 <w:br w:type="page"/>
     * 制表符 <w:tab/>
     * @throws \PhpOffice\PhpWord\Exception\CopyFileException
     * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
     */
    public static function create($dataArray,$templateFilePath = '',$isDownload = true){

        $uploadDir = './upload/word/';
        empty($templateFilePath) && $templateFilePath = './Modules/chinadmin/word/order.docx';

        //打开模板
        $tmp = new TemplateProcessor($templateFilePath);
        $tmp->setValues($dataArray);
        // 设置图片
        //$tmp->setImageValue('header', ['path' => '1.jpg','width'=>500,'height'=>500],3);
        //另存为
        $filename = 'word-' . date('YmdHis') . '.doc';
        $filePath = $uploadDir . $filename;
        $tmp->saveAs($filePath);
        if($isDownload){
            // 浏览器下载
            ob_clean();
            ob_start();
            $fp = fopen($filePath,"r");
            $file_size = filesize($filePath);
            Header("Content-type:application/octet-stream");
            Header("Accept-Ranges:bytes");
            Header("Accept-Length:" . $file_size);
            Header("Content-Disposition:attchment; filename=" . $filename);
            $buffer = 1024;
            $file_count = 0;
            while(!feof($fp) && $file_count < $file_size){
                $file_con = fread($fp,$buffer);
                $file_count += $buffer;
                echo $file_con;
            }
            fclose($fp);
            ob_end_flush();
            exit();
        }
        return $filePath;

    }


    /**
     * 填充html导出word
     * @param $filename
     * @param $html
     * @throws \PhpOffice\PhpWord\Exception\Exception
     */
    public static function export($filename,$html){
        $title = 'aa';
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Disposition: attachment;filename="' . $title . '.docx"');
        header('Cache-Control: max-age=0');
        //html信息
        $html = '<div>
           <table>
            <tr>
                <td>11111111111</td>
        </tr>
        <tr>
                <td>22222</td>
        </tr>
        </table>
        </div>';
        $phpWord = new PhpWord();
        $section = $phpWord->addSection();
        Html::addHtml($section,$html,false,false);
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord,'Word2007');
        $objWriter->save('php://output');
        exit();
    }


    private function download(){

    }
}
